// Import the set of Radius resources (Applications.*) into Bicep
extension radius
// import radius as radius
@description('The ID of your Radius Application. Set automatically by the rad CLI.')
param application string

resource gateway 'Applications.Core/gateways@2023-10-01-preview' = {
  name: 'gateway'
  properties: {
    application: application
    hostname: {
      // Omitting hostname properties results in gatewayname.appname.PUBLIC_HOSTNAME_OR_IP.nip.io

      // Results in prefix.appname.PUBLIC_HOSTNAME_OR_IP.nip.io
      prefix: 'prefix'
      // Alternately you can specify your own hostname that you've configured externally
      fullyQualifiedHostname: 'demo.youling.io'
    }
    routes: [
      {
        path: '/'
        destination: 'http://${frontend_web.name}:3000'
      }
      {
        path: '/'
        destination: 'http://${frontend_app.name}:3000'
      }
    ]
  }
}
resource frontend_app 'Applications.Core/containers@2023-10-01-preview' = {
  name: 'frontend_app'
  properties: {
    application: application
    container: {
      image: 'ghcr.io/radius-project/samples/demo:latest'
      ports: {
        web: {
          containerPort: 3000
        }
      }
    }
    connections: {
      // mongodb: {
      //   source: mongodb.id
      // }
      backend: {
        source: 'http://${backend.name}:80'
      }
    }

  }
}
resource frontend_web 'Applications.Core/containers@2023-10-01-preview' = {
  name: 'frontend_web'
  properties: {
    application: application
    container: {
      image: 'ghcr.io/radius-project/samples/demo:latest'
      ports: {
        web: {
          containerPort: 3000
        }
      }
    }
    connections: {
      // mongodb: {
      //   source: mongodb.id
      // }
      backend: {
        source: 'http://${backend.name}:80'
      }
    }

  }
}

resource backend 'Applications.Core/containers@2023-10-01-preview' = {
  name: 'backend'
  properties: {
    application: application
    container: {
      image: 'nginx:latest'
      ports: {
        api: {
          containerPort: 80
        }
      }
    }
    connections: {
      mysql: {
        source: mongodb.id
      }
      // backend: {
      //   source: 'http://${backend.name}:80'
      // }
    }
  }

}
@description('The ID of your Radius Environment. Set automatically by the rad CLI.')
param environment string

resource mongodb 'Applications.Datastores/mongoDatabases@2023-10-01-preview' = {
  name: 'mongodb'
  properties: {
    environment: environment
    application: application
  }
}
